import Vue from 'vue'
import VueRouter from 'vue-router'
import ViewUI from 'view-design';

Vue.use(VueRouter)


const Login = () => import('../views/login/Login')
const Index = () => import('../views/index/Index')
const Classification = () => import('../views/classification/Classification')
const Journal = () => import('../views/journal/Journal')
const Write = () => import('../views/blog/write/Write')
const Manage = () => import('../views/blog/manage/Manage')
const Draft = () => import('../views/blog/draft/Draft')
const ArticleDetail = () => import('../views/articleDetail/ArticleDetail')
const AboutMe = () => import('../views/aboutMe/AboutMe')
const LimitPhone = () => import('../views/limit/LimitPhone')

  const routes = [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/',
      name: "Index",
      component: Index,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/classification',
      name: 'Classification',
      component: Classification,
      meta: {
        title: '分类'
      }
    },
    {
      path: '/journal',
      name: 'Journal',
      component: Journal,
      meta: {
        title: '日志'
      }
    },
    {
      path: '/write',
      name: 'Write',
      component: Write,
      meta: {
        title: '写博客'
      }
    },
    {
      path: '/manage',
      name: 'Manage',
      component: Manage,
      meta: {
        title: '文章管理'
      }
    },
    {
      path: '/draft',
      name: 'Draft',
      component: Draft,
      meta: {
        title: '草稿箱'
      }
    },
    {
      path: '/articleDetail',
      name: 'ArticleDetail',
      component: ArticleDetail,
      meta: {
        title: '文章详细'
      }
    },
    {
      path: '/aboutMe',
      name: 'AboutMe',
      component: AboutMe,
      meta: {
        title: '关于我'
      }
    },
    {
      path: '/limitPhone',
      name: 'LimitPhone',
      component: LimitPhone,
      meta: {
        title: '手机不支持！！！'
      }
    }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

router.beforeEach(((to, from, next) =>{
      document.title = to.matched[0].meta.title
      ViewUI.LoadingBar.start();
      const width = document.body.clientWidth
      if (width <= 1200 && to.name !== 'LimitPhone') {
        next('/limitPhone');
      } else {
        next();
      }

    }
))

router.afterEach(() => {
  ViewUI.LoadingBar.finish();
  window.pageYOffset = 0
  document.body.scrollTop = 0
  document.documentElement.scrollTop = 0
});

export default router
